import sys

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (QApplication, QMainWindow, QListWidget,
                             QStackedWidget, QHBoxLayout, QWidget,
                             QListWidgetItem, QLabel)


class LeftMenuWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("左侧菜单示例")
        self.setGeometry(100, 100, 800, 600)

        # 创建主布局
        main_widget = QWidget()
        main_layout = QHBoxLayout(main_widget)
        main_layout.setContentsMargins(0, 0, 0, 0)
        main_layout.setSpacing(0)

        # 创建左侧菜单
        self.left_menu = QListWidget()
        self.left_menu.setFixedWidth(180)  # 设置左侧菜单宽度
        self.left_menu.setStyleSheet("""
            QListWidget {
                background-color: #f0f0f0;
                border: none;
            }
            QListWidget::item {
                height: 40px;
                padding-left: 20px;
            }
            QListWidget::item:selected {
                background-color: #e0e0e0;
                border-left: 4px solid #0078d7;
            }
        """)

        # 添加菜单项
        menu_items = ["首页", "设置", "数据报表", "帮助中心", "关于我们"]
        for item in menu_items:
            self.left_menu.addItem(QListWidgetItem(item))

        # 创建右侧内容区域
        self.stacked_widget = QStackedWidget()

        # 添加页面到堆叠窗口
        page_titles = ["欢迎使用首页", "系统设置", "数据报表展示", "帮助文档", "关于信息"]
        for title in page_titles:
            page = QLabel(title, self)
            page.setAlignment(Qt.AlignCenter)
            page.setStyleSheet("font-size: 24px;")
            self.stacked_widget.addWidget(page)

        # 将菜单和内容添加到主布局
        main_layout.addWidget(self.left_menu)
        main_layout.addWidget(self.stacked_widget)

        # 设置中心部件
        self.setCentralWidget(main_widget)

        # 连接信号槽
        self.left_menu.currentRowChanged.connect(self.stacked_widget.setCurrentIndex)

        # 默认选中第一项
        self.left_menu.setCurrentRow(0)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = LeftMenuWindow()
    window.show()
    sys.exit(app.exec_())